<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/5/30
 * Time: 14:43
 * 素材
 */

namespace app\service;


use app\cls\tom\Praise;
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\model\UserMaterial;

class Material
{
    public function getUserMaterialNums(array &$rows)
    {
        $userMaterialModel = new UserMaterial(SysResource::getDB(false));
        foreach($rows as &$row){
            $row["video_nums"] = $userMaterialModel->getUserMaterialNums($row["user_uuid"]);
        }
    }

    /**
     * 获取视频用户动态 对该视频点赞和录音过的用户信息
     * @param array $rows
     */
    private function getVideoDy(array &$rows)
    {
        $praiseKeyPrefix = REDIS_PREFIX."video_dy_";
        $materialKeyPrefix = REDIS_PREFIX."material_dy_";
        $redis = SysResource::getRedis();
        $db = SysResource::getDB(false);
        foreach($rows as &$row){
            $key = $praiseKeyPrefix.$row["user_material_uuid"];
            $key1 = $materialKeyPrefix.$row["ori_material_uuid"];
            //用户点赞 取最近10个
            $praiseUserUUIDList = $redis->zRevRange($key,0,9,true);
            $newPraiseUserInfoList = [];
            if($praiseUserUUIDList){
                $newTempPraiseUserInfoList = $db->select("user_uuid,nickname,head_image,1 as type")->from("users")->where(["user_uuid"=>array_keys($praiseUserUUIDList)])->get()->all();
                foreach($praiseUserUUIDList as $userUUID=>$addTime){
                    foreach($newTempPraiseUserInfoList as $tempUserInfo){
                        if($tempUserInfo["user_uuid"] == $userUUID){
                            $tempUserInfo["add_time"] = $addTime;
                            $newPraiseUserInfoList[] = $tempUserInfo;
                        }
                    }
                }
            }
            //拉取此视频的10录音用户
            $materialUserUUIDList = $redis->zRevRange($key1,0,9,true);
            $newMaterialUserInfoList = [];
            if($materialUserUUIDList){
                $newTempMaterialUserInfoList = $db->select("user_uuid,nickname,head_image,2 as type")->from("users")->where(["user_uuid"=>array_keys($materialUserUUIDList)])->get()->all();
                foreach($materialUserUUIDList as $userUUID=>$addTime){
                    foreach($newTempMaterialUserInfoList as $tempUserInfo){
                        if($tempUserInfo["user_uuid"] == $userUUID){
                            $tempUserInfo["add_time"] = $addTime;
                            $newMaterialUserInfoList[] = $tempUserInfo;
                        }
                    }
                }
            }
            $allInfo = array_merge($newPraiseUserInfoList,$newMaterialUserInfoList);
            //排序
            uasort($allInfo,function($a,$b){
                if ($a["add_time"] == $b["add_time"]) {
                    return 0;
                }
                return ($a["add_time"] < $b["add_time"]) ? 1 : -1;
            });
            $arr = array_chunk($allInfo,10);
            if($arr){
                $row["dy"] = $arr[0];
            }else{
                $row["dy"] = [];
            }
        }
    }


    /**
     * 热门列表
     * @param $userUUID
     * @param $page
     * @param $pageSize
     * @return Response
     * @throws \Exception
     */
    public function hotList($userUUID,$page, $pageSize)
    {
        $key = REDIS_PREFIX."hot_list_{$pageSize}_{$page}";
        $redis = SysResource::getRedis();
        $data = $redis->get($key);
        if($data){
            $data = json_decode($data,true);
        }else{
            $db = SysResource::getDB(true);
            $materialModel = new UserMaterial($db);
            $data = $materialModel->getInterfaceMaterialList(["a.status1"=>1,"a.status"=>2],["a.custom_order"=>"asc"],$page,$pageSize);
            $this->getVideoDy($data["data"]);
            $redis->setEx($key,30,json_encode($data));
        }
        $praiseObj = new Praise($userUUID,"",SysResource::getRedis(),"video");
        foreach($data["data"] as &$material){
            if($userUUID){
                $praiseObj->setIdentity($material["user_material_uuid"]);
                $clickInfo = $praiseObj->getClickPraiseInfo();
                $material["is_praise"] = $clickInfo["is_praise"];
            }else{
                $material["is_praise"] = 0;
            }
        }
        return new Response(0,"ok",$data["data"],$data["total"]);
    }


    /**
     * 获取用户的视频
     * @param $userUUID
     * @param $page
     * @param $pageSize
     * @param bool $isSelf 是否本人查看
     * @return Response
     */
    public function userVideoList($userUUID,$page,$pageSize,$isSelf = false)
    {
        $key = REDIS_PREFIX."user_list_{$userUUID}_{$pageSize}_{$page}";
        $redis = SysResource::getRedis();
        $data = $redis->get($key);
        if($data){
            $data = json_decode($data,true);
        }else{
            $db = SysResource::getDB(false);
            $materialModel = new UserMaterial($db);
            $where = ["a.user_uuid"=>$userUUID];
            if(!$isSelf){
                $where["a.status"] = 2;
                $where["a.status1"] = 1;
            }
            $data = $materialModel->getInterfaceMaterialList(["a.user_uuid"=>$userUUID],["a.order_zan"=>"desc","a.id"=>"desc"],$page,$pageSize);
            $this->getVideoDy($data["data"]);
            $redis->setEx($key,5,json_encode($data));
        }
        return new Response(0,"ok",$data["data"],$data["total"]);
    }

    /**
     * 用户视频详情
     * @param $userMaterialUUID
     * @return Response
     */
    public function userVideoDetail($userMaterialUUID)
    {
        $key = REDIS_PREFIX."user_video_{$userMaterialUUID}";
        $redis = SysResource::getRedis();
        $data = $redis->get($key);
        if($data){
            $data = json_decode($data,true);
        }else{
            $db = SysResource::getDB(false);
            $materialModel = new UserMaterial($db);
            $data = $materialModel->getMaterialDetail($userMaterialUUID);
            $redis->setEx($key,50,json_encode($data));
        }
        return new Response(0,"ok",$data);
    }

    /**
     * 使用录音素材录音的视频
     * @param $material_id string 素材UUID
     * @param $page int 页数
     * @param $pageSize
     * @return Response
     */
    public function getMaterialRecordList($material_id, $page, $pageSize)
    {
        $key = REDIS_PREFIX."record_video_list_{$material_id}_{$pageSize}_{$page}";
        $redis = SysResource::getRedis();
        $data = $redis->get($key);
        if($data){
            $data = json_decode($data,true);
        }else{
            $db = SysResource::getDB(false);
            $materialModel = new UserMaterial($db);
            $data = $materialModel->getInterfaceMaterialList(["a.status1"=>1,"a.status"=>2,"a.ori_material_uuid"=>$material_id],["order_zan"=>"desc","a.id"=>"desc"],$page,$pageSize);
            $redis->setEx($key,100,json_encode($data));
        }
        return new Response(0,"ok",$data["data"],$data["total"]);
    }

    /**
     * 底部推荐视频
     * @return Response
     */
    public function recommendV()
    {
        $key = REDIS_PREFIX."record_video_recommendV";
        $redis = SysResource::getRedis();
        $data = $redis->get($key);
        if($data){
            $data = json_decode($data,true);
        }else{
            $db = SysResource::getDB(false);
            $db->select("material_id,title,background as image,add_time");
            $db->from("toolbar");
            $data = $db->get()->all();
            $redis->setEx($key,100,json_encode($data));
        }
        return new Response(0,"ok",$data);
    }
}